import { Token, Channel, Histories } from '@/types/data'

const TOKEN_KEY = 'react-project'
const CHANNEL_KEY = 'react-project-channel'
const HISTORY_KEY = 'react-project-history'

/**
 * 保存token
 * @param token
 */
export function setToken(token: Token): void {
  localStorage.setItem(TOKEN_KEY, JSON.stringify(token))
}

/**
 * 获取token
 * @returns Token
 */
export function getToken(): Token {
  return JSON.parse(localStorage.getItem(TOKEN_KEY) || '{}')
}
/**
 * 删除token
 */
export function removeToken(): void {
  localStorage.removeItem(TOKEN_KEY)
}

/**
 * 判断本地存储是否有token
 * @returns boolean
 */
export function hasToken(): boolean {
  return !!getToken().token
}

/**
 * 保存频道数据到本地
 * @param channels
 */
export function setChannels(channels: Array<Channel>): void {
  localStorage.setItem(CHANNEL_KEY, JSON.stringify(channels))
}
/**
 * 从本地获取频道列表数据
 * @returns channels
 */
export function getChannels(): Array<Channel> {
  return JSON.parse(localStorage.getItem(CHANNEL_KEY) || '[]')
}
/**
 * 保存历史数据到本地
 * @param histories
 */
export function setHistoryStorage(histories: Histories) {
  localStorage.setItem(HISTORY_KEY, JSON.stringify(histories))
}
/**
 * 获取本地历史数据
 * @returns histories
 */
export function getHistoryStorage() {
  return JSON.parse(localStorage.getItem(HISTORY_KEY) || '[]')
}
